package io.github.xbeeant.eoffice.config.security;

import io.github.xbeeant.core.ApiResponse;
import io.github.xbeeant.eoffice.model.User;
import io.github.xbeeant.eoffice.service.IUserService;
import io.github.xbeeant.eoffice.util.SecurityHelper;
import io.github.xbeeant.spring.web.SpringContextProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaobiao
 * @version 2021/7/8
 */
public class TokenAuthenticationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String token = request.getParameter("token");
        if (token != null) {
            String userId = SecurityHelper.tokenToUserId(token);
            if (userId != null && SecurityContextHolder.getContext().getAuthentication() == null) {
                ApiResponse<User> userApiResponse = SpringContextProvider.getBean(IUserService.class).selectByPrimaryKey(Long.valueOf(userId));
                UserDetails userDetails = SpringContextProvider.getBean(IUserService.class).loadUserByUsername(userApiResponse.getData().getUsername());
                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
        }
        filterChain.doFilter(request, response);
    }
}
